//
//  JWCalendarCell.m
//  SiqiCalendarPro
//
//  Created by sunjiawen on 2017/9/1.
//  Copyright © 2017年 sjw. All rights reserved.
//

#import "JWCalendarCell.h"
#import "UIView+Extension.h"
#import "JWScheduleModel.h"

@implementation JWCalendarCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {

        
        self.shapeLayer.hidden = YES;
        self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        self.backgroundView.backgroundColor = [UIColor clearColor];

        self.turnLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        self.turnLabel.backgroundColor = [UIColor clearColor];
        self.turnLabel.textColor = [UIColor whiteColor];
        self.turnLabel.textAlignment = NSTextAlignmentCenter;
        [self.turnLabel setAdjustsFontSizeToFitWidth:YES];
        self.turnLabel.numberOfLines = 0;
        self.turnLabel.layer.cornerRadius = 5;
        self.turnLabel.layer.masksToBounds = NO;
        self.turnLabel.text = @"休";
        [self.backgroundView addSubview:self.turnLabel];
        

        self.chuanbanImageView = [[UIImageView alloc]init];
        self.chuanbanImageView.image = [UIImage imageNamed:@"icon_chuanban"];
        self.chuanbanImageView.hidden = YES;
        [self.turnLabel addSubview:self.chuanbanImageView];
        
        self.todayClickImageView = [[UIImageView alloc]init];
        self.todayClickImageView.image = [UIImage imageNamed:@"cursor8"];
        self.todayClickImageView.hidden = YES;
        [self.backgroundView addSubview:self.todayClickImageView];
        
        self.selectImageView = [[UIImageView alloc]init];
        self.selectImageView.image = [UIImage imageNamed:@"cursor6"];
        self.selectImageView.hidden = YES;
        [self.backgroundView addSubview:self.selectImageView];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

//    [self setNeedsLayout];
    
    self.backgroundView.frame = CGRectIntegral(self.bounds);
    
    self.turnLabel.frame = CGRectMake(self.backgroundView.m_width - WGiveWidth(5) - WGiveWidth(16), WGiveHeight(6), WGiveWidth(16), self.backgroundView.m_height - WGiveWidth(5) - WGiveWidth(13));
    self.titleLabel.frame = CGRectMake(WGiveWidth(5), WGiveHeight(7), self.backgroundView.m_width - self.turnLabel.m_x - WGiveWidth(5), WGiveWidth(20));
    self.subtitleLabel.frame = CGRectMake(self.titleLabel.m_x, CGRectGetMaxY(self.titleLabel.frame), CGRectGetWidth(self.titleLabel.frame), CGRectGetMaxY(self.turnLabel.frame) - CGRectGetMaxY(self.titleLabel.frame));
    
    CGFloat imgWH = self.turnLabel.m_width/3 * 2;
    self.chuanbanImageView.frame = CGRectMake(self.turnLabel.m_width - imgWH, 0, imgWH,imgWH);
    
    self.todayClickImageView.frame = self.backgroundView.bounds;
    self.selectImageView.frame = self.backgroundView.bounds;
}

- (void)configureAppearance {
    [super configureAppearance];
    
    if (self.isPlaceholder) {
        self.titleLabel.textColor = [UIColor warmGrayColor];
    }else {
        self.titleLabel.textColor = self.weekend ? [UIColor indianRedColor] : [UIColor blackColor];
    }
    
    self.titleLabel.font = [UIFont fontWithName:@"Futura-CondensedMedium" size:18];
    self.backgroundColor = [UIColor clearColor];
    self.subtitleLabel.textColor = [ChineseFestival containsObject:self.subtitle] ? [UIColor indianRedColor] : [UIColor colorFromHexString:@"#aab2bd"];

    [self.subtitleLabel setAdjustsFontSizeToFitWidth:YES];
    
    self.selectImageView.hidden = !self.isSelected;
    self.todayClickImageView.hidden = !self.dateIsToday;
    
}
    

- (void)setDataModel:(JWScheduleModel *)dataModel {
    _dataModel = dataModel;
    
    if (dataModel) {
        if (dataModel.scheduleName.length > 1) {
            self.turnLabel.text = [self dealWithString:dataModel.scheduleName];
        }else {
            self.turnLabel.text = dataModel.scheduleName;
        }
        
        self.turnLabel.layer.backgroundColor = dataModel.scheduleColor.CGColor;
    }else {
        self.turnLabel.text = @"无";
        self.turnLabel.layer.backgroundColor = [UIColor clearColor].CGColor;
    }
}

//每个字符添加一个换行的字符串算法
- (NSString *)dealWithString:(NSString *)number
{
    NSString *doneTitle = @"";
    int count = 0;
    for (int i = 0; i < number.length; i++) {
        
        count++;
        doneTitle = [doneTitle stringByAppendingString:[number substringWithRange:NSMakeRange(i, 1)]];
        if (count == 1) {
            if (i == number.length - 1) {
                doneTitle = [NSString stringWithFormat:@"%@", doneTitle];
            }else {
                doneTitle = [NSString stringWithFormat:@"%@\n", doneTitle];
            }
            
            count = 0;
        }
    }
    NSLog(@"%@", doneTitle);
    return doneTitle;
}
@end
